package com.sinux.generality.basesupport.utils.tool;

import org.apache.commons.lang3.StringUtils;

/**
 * 进制操作
 * 
 * @ClassName BinaryUtil
 * @Description: 进制操作
 * @author Tangjc
 * @date 2021年5月19日 下午2:55:15
 */
public class BinaryUtil {

	/**
	 * 字符串转换成为16进制(无需Unicode编码)
	 * 
	 * @param str
	 * @return
	 */
	public static String str2HexStr(String str) {
		char[] chars = "0123456789ABCDEF".toCharArray();
		StringBuilder sb = new StringBuilder("");
		byte[] bs = str.getBytes();
		int bit;
		for (int i = 0; i < bs.length; i++) {
			bit = (bs[i] & 0x0f0) >> 4;
			sb.append(chars[bit]);
			bit = bs[i] & 0x0f;
			sb.append(chars[bit]);
			// sb.append(' ');
		}
		return sb.toString().trim();
	}

	/**
	 * 16进制直接转换成为字符串(无需Unicode解码)
	 * 
	 * @param hexStr
	 * @return
	 */
	public static String hexStr2Str(String hexStr) {
		String str = "0123456789ABCDEF";
		char[] hexs = hexStr.toCharArray();
		byte[] bytes = new byte[hexStr.length() / 2];
		int n;
		for (int i = 0; i < bytes.length; i++) {
			n = str.indexOf(hexs[2 * i]) * 16;
			n += str.indexOf(hexs[2 * i + 1]);
			bytes[i] = (byte) (n & 0xff);
		}
		return new String(bytes);
	}

	public static String bytesToHexString(byte[] src) {
		StringBuilder stringBuilder = new StringBuilder("");
		if (src == null || src.length <= 0) {
			return null;
		}
		for (int i = 0; i < src.length; i++) {
			int v = src[i] & 0xFF;
			String hv = Integer.toHexString(v);
			if (hv.length() < 2) {
				stringBuilder.append(0);
			}
			stringBuilder.append(hv);
		}
		return stringBuilder.toString();
	}

	/**
	 * Convert hex string to byte[]
	 * 
	 * @param hexString
	 *            the hex string
	 * @return byte[]
	 */
	public static byte[] hexStringToBytes(String hexString) {
		if (hexString == null || hexString.equals("")) {
			return null;
		}
		hexString = hexString.toUpperCase();
		int length = hexString.length() / 2;
		char[] hexChars = hexString.toCharArray();
		byte[] d = new byte[length];
		for (int i = 0; i < length; i++) {
			int pos = i * 2;
			d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
		}
		return d;
	}

	public static byte hexStringToByte(String hexString) {
		hexString = hexString.toUpperCase();
		char[] hexChars = hexString.toCharArray();
		byte d = (byte) (charToByte(hexChars[0]) << 4 | charToByte(hexChars[1]));
		return d;
	}

	private static byte charToByte(char c) {
		return (byte) "0123456789ABCDEF".indexOf(c);
	}

	/**
	 * 字符串转换为Ascii
	 * 
	 * @param value
	 * @return
	 */
	public static String asciiBe(String value) {
		StringBuffer sbu = new StringBuffer();
		char[] chars = value.toCharArray();
		for (int i = 0; i < chars.length; i++) {
			if (i != chars.length - 1) {
				sbu.append((int) chars[i]).append(",");
			} else {
				sbu.append((int) chars[i]);
			}
		}
		return sbu.toString();
	}

	/**
	 * Ascii转换为字符串
	 * 
	 * @Title: asciiRe
	 * @Description: Ascii转换为字符串
	 * @author Tangjc
	 * @param value
	 *            ascii码
	 * @return
	 * @return String
	 */
	public static String asciiRe(String value) {
		StringBuffer sbu = new StringBuffer();
		String[] chars = value.split(",");
		for (int i = 0; i < chars.length; i++) {
			sbu.append((char) Integer.parseInt(chars[i]));
		}
		return sbu.toString();
	}

	public static String fillZero(StringBuilder value, int len, String way){
		while(value.length()<len*2){
			if(StringUtils.equals("right", way)){
				rightFillZero(value);
			}else{
				leftFillZero(value);
			}
		}
		return value.toString();
	}
	
	public static String fillZero(String valueStr, int len, String way){
		StringBuilder value = new StringBuilder(valueStr);
		while(value.length()<len*2){
			if(StringUtils.equals("right", way)){
				rightFillZero(value);
			}else{
				leftFillZero(value);
			}
		}
		return value.toString();
	}
	
	public static String fillZero(int valueInt, int len, String way){
		StringBuilder value = new StringBuilder(String.valueOf(valueInt));
		while(value.length()<len*2){
			if(StringUtils.equals("right", way)){
				rightFillZero(value);
			}else{
				leftFillZero(value);
			}
		}
		return value.toString();
	}
	
	public static void leftFillZero(StringBuilder value){
		value.insert(0, "0");
	}
	
	public static void rightFillZero(StringBuilder value){
		value.append("0");
	}
	
	public static String binaryStr2Hexstr(String binaryStr){
		StringBuilder sb = new StringBuilder();
		for(int i=0; i<binaryStr.length();i+=8){
			int a = Integer.parseInt(binaryStr.substring(i, i+8), 2);
			sb.append(fillZero(Integer.toHexString(a), 1, "left"));
		}
		return sb.toString();
	}
	
	public static String binaryStr2Decstr(String binaryStr){
		StringBuilder sb = new StringBuilder();
		for(int i=0; i<binaryStr.length();i+=8){
			int a = Integer.parseInt(binaryStr.substring(i, i+8), 2);
			sb.append(fillZero(a, 1, "left"));
		}
		return sb.toString();
	}
	/**
	 * 低在前 int
	 * @Title: toLh
	 * @Description: TODO
	 * @author Tangjc
	 * @param n
	 * @return
	 * @return byte[]
	 */
	public static byte[] toLh(int n){
		byte[] b = new byte[4];
		b[0] = (byte)(n & 0xff);
		b[1] = (byte)(n >> 8 & 0xff);
		b[2] = (byte)(n >> 16 & 0xff);
		b[3] = (byte)(n >> 24 & 0xff);
		return b;
	}
	
	/**
	 * 高在前 int
	 * @Title: toHH
	 * @Description: TODO
	 * @author Tangjc
	 * @param n
	 * @return
	 * @return byte[]
	 */
	public static byte[] toHH(int n){
		byte[] b = new byte[4];
		b[3] = (byte)(n & 0xff);
		b[2] = (byte)(n >> 8 & 0xff);
		b[1] = (byte)(n >> 16 & 0xff);
		b[0] = (byte)(n >> 24 & 0xff);
		return b;
	}
	
	/**
	 * 低在前 long
	 * @Title: getBytes
	 * @Description: TODO
	 * @author Tangjc
	 * @param n
	 * @return
	 * @return byte[]
	 */
	public static byte[] getBytes(long n){
		byte[] b = new byte[8];
		b[0] = (byte)(n & 0xff);
		b[1] = (byte)(n >> 8 & 0xff);
		b[2] = (byte)(n >> 16 & 0xff);
		b[3] = (byte)(n >> 24 & 0xff);
		b[4] = (byte)(n >> 32 & 0xff);
		b[5] = (byte)(n >> 40 & 0xff);
		b[6] = (byte)(n >> 48 & 0xff);
		b[7] = (byte)(n >> 56 & 0xff);
		
		return b;
	}
	public static void main(String[] args) {
		System.out.println(Integer.toHexString(92));
		byte b = hexStringToByte("5C");
		byte bb[] = hexStringToBytes("5C");
		System.out.println(b);
		System.out.println(bb.length);
	}
}
